﻿using System;
using System.Xml.Serialization;
using System.ComponentModel;

namespace ICeFramework.Utilities.Text
{
    public static class Strings
    {
        public static string FindAndReplace(string text, string findText, string replaceText, FindOptions options)
        {
            if (string.IsNullOrEmpty(findText) || options == FindOptions.None)
            {
                return text;
            }
            string find = findText;
            string conditionText = text;
            if ((options & FindOptions.MatchCase) != FindOptions.MatchCase)
            {
                find = find.ToLower();
                conditionText = text.ToLower();
            }
            int index = conditionText.IndexOf(find);
            if ((options & FindOptions.Contains) == FindOptions.Contains)
            {
                if (index >= 0)
                {
                    text = text.Substring(0, index) + replaceText + text.Substring(index + findText.Length);
                }
                return text;
            }
            if ((options & FindOptions.Begin) == FindOptions.Begin)
            {
                if (index == 0)
                {
                    text = replaceText + text.Substring(findText.Length);
                }
            }
            if ((options & FindOptions.End) == FindOptions.End)
            {
                if (index + find.Length == text.Length)
                {
                    text = text.Substring(0, text.Length - findText.Length) + replaceText;
                }
            }

            return text;
        }
    }
}
